#include "StdAfx.h"
#include "BlastPile2010.h"

#include "MatchingOreAddIn.h"
#include "Storager.h"
#include "BlastPileDBManager.h"

CBlastPile2010::CBlastPile2010(CdmDB* DisplayDB)
	: m_pDisplayDB(DisplayDB)	
	, m_iProgramCount(0)
	, m_dMaxCapacity(0)
	, m_pRegionDBManager(NULL)
{
	m_pRegionDBManager = new CRegionManager(m_pDisplayDB);
}

CBlastPile2010::~CBlastPile2010(void)
{
}

CPolylineEntity* CBlastPile2010::GetBoundaryPLEnt() const
{
	if (NULL == m_pDisplayDB)
		return NULL;

	CdmLayer* pBoundaryLayer = m_pDisplayDB->GetLayerFromName(BLAST_PILE_BOUNDARY_LAYER);
	if (NULL == pBoundaryLayer)
		return NULL;

	CdmEntity* pEntity = NULL;
	HC_KEY key = -1;
	pBoundaryLayer->StartQueryEntity();
	while (pBoundaryLayer->GetNextEntity(&pEntity,&key))
	{
		if (pEntity->IsDeleted() || pEntity->GetType()!=ETT_POLYLINE)
		{
			continue;
		}
		else
		{
			break;
		}
	}///:~while

	return (CPolylineEntity*)pEntity;
}

CDataTable* CBlastPile2010::GetBlastHoleTable() const
{
	if (NULL == m_pDisplayDB)
		return NULL;

	return m_pDisplayDB->GetDataTable();
}

bool CBlastPile::GetBlockData(std::vector<CRegionBlock>& vRegionBlk)		
{
	if (NULL == m_pRegionDBManager)
		return false;

	CEntityArray blkEntArr;
	bool bRes = m_pRegionDBManager->GetBlkEntArr(blkEntArr);	
	if (!bRes)
		return false;

	if (m_vRegionBlk.empty())
	{
		BuildAllBlk(blkEntArr, m_pRegionDBManager->GetColCount());
	}

	vRegionBlk = m_vRegionBlk;

	return true;
}

bool CBlastPile::UpdateDB(std::vector<CRegionBlock>& vecBlock)
{
	if (vecBlock.empty())
		return false;

	if (NULL == m_pRegionDB || m_vRegionBlk.empty())
		return false;

	int iBlockNum = (int)m_vRegionBlk.size();
	if (iBlockNum != vecBlock.size())
		return false;

	m_vRegionBlk = vecBlock;

	CRegionBlock block;
	for (int i=0; i<iBlockNum; ++i)
	{
		block = m_vRegionBlk.at(i);
		std::vector<CdmEntity*> vBlkEnt;
		block.GetEntArr(vBlkEnt);
		for (int j=0; j<(int)vBlkEnt.size(); ++j)
		{
			CdmEntity* pEnt = vBlkEnt.at(i);
			if (pEnt != NULL)
			{
				CAttRecord* pAttRec = pEnt->GetAttRecord();
				pAttRec->Set_Value(MINING_TIME_FLD, block.GetSolutionValue());	
			}
		}///:~for--j
	}///:~for--i

	m_pRegionDB->Save();

	return true;
}